package com.project.service.impl;

import com.project.bean.InfoBean;
import com.project.bean.MQBean;
import org.springframework.amqp.core.Message;

import com.project.remoteService.IInfoService;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RabbitService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    IInfoService service;
    RabbitTemplate.ConfirmCallback callback=(CorrelationData correlationData, boolean b, String s)->{
        if (b){
//            service.update(correlationData.getId(),"已出票");
        }
    };
    //默认
    public void  sendUser(MQBean infoBean){
        //开启手动确认
        rabbitTemplate.setMandatory(true);//开启消息确认
        //绑定回调函数
        rabbitTemplate.setConfirmCallback(callback);
        //创建关联数据对象
        CorrelationData correlationData=new CorrelationData(infoBean.getId());

        rabbitTemplate.convertAndSend("directExchangeInfo","info.i",infoBean,correlationData);
    }
    //主题
    public void  sendUserTopic(String str){
        rabbitTemplate.convertAndSend("topicExchange","info.c",str);
    }
    public void  sendFanout(String str){
        rabbitTemplate.convertAndSend("fanoutExchange",null,str);
    }
    public void  sendHeader(String str){
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("orderKey", "order");
        messageProperties.setHeader("dateKey", "2022-12-12");
        Message message = new Message(str.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend("headersExchange", null, message);
    }
    public void  sendJ187(String str)
    {
        rabbitTemplate.convertAndSend("directExchangeJ187","j187",str);
    }
}
